package untils

import (
	"github.com/google/uuid"
	capi "github.com/hashicorp/consul/api"
)

type ConsulClient struct {
	client *capi.Client
}

func NewConsulClient(address string) (*ConsulClient, error) {
	config := capi.DefaultConfig()
	config.Address = address
	client, err := capi.NewClient(config)
	if err != nil {
		panic(err)
	}
	return &ConsulClient{client: client}, nil
}

type GRPCService struct {
	Name    string
	Tags    []string
	Address string
	Port    int
}

func (c *ConsulClient) RegisterService(cfg *GRPCService) error {
	req := capi.AgentServiceRegistration{
		ID:      uuid.NewString(),
		Name:    cfg.Name,
		Tags:    cfg.Tags,
		Port:    cfg.Port,
		Address: cfg.Address,
	}
	return c.client.Agent().ServiceRegister(&req)
}
